var app = getApp();
Page({
    data: {
        nowDate:"",         // 当前选择年月
        now_date:"",        // 系统当前日期
        nowDay:0,           // 当前日期

        selectedDate:"",    // 选择日期 - 年月
        selectedDay:[],     // 选中日期 - 日
        selectedDayList:[], // 选择
        dateTimeList:"",    // 置顶日期数据

        weekList:["日","一","二","三","四","五","六"],

        week:0,             // 当前月份第一天属于星期几
        day:[],             // 当前月份天数          
        days:[],            // 总天数 加上星期

        auction_id:"",      // 活动id
        rank_id:"",         // 排名
        price:0,            // 订单价格
    },
    // 生命周期函数--监听页面加载
    onLoad: function (options) {
        var myDate = new Date();
        var year = myDate.getFullYear();
        var month = myDate.getMonth();
        var nowDay = myDate.getDate();
        var nowDate = year + "-" + ( month + 1 >= 10 ? (month + 1) : "0" + (month + 1) );


        this.setData({
            nowDate,
            nowDate, 
            nowDay,
            now_date: nowDate,
			type: options.type ? options.type : "", 
            auction_id: options.auction_id ? options.auction_id : 0,
            rank_id: options.rank_id ? options.rank_id : 1
        })
        this.computeDate(year, month + 1)
    },
    // 选中日期
    selectDay(e){
        var item_this = e.target.dataset.item;
        var index = e.target.dataset.index;
        var nowDate = this.data.nowDate;
        var days = this.data.days;

        days.map(function(item,index){
            if (item_this == item.date){
                days[index].is_chacked = !days[index].is_chacked
            }
        })

        this.setData({ 
            selectedDate : nowDate,
            days: days
        })
        this.computePrice();
    },
    // 计算订单金额
    computePrice(){
        var self = this;
        var url = "/user/get_price";
        var days = this.data.days;
        var rank_id = this.data.rank_id;
        var selectedDayList = [];
        days.map(function(item,index){
            if(item.is_chacked){
                selectedDayList.push(item.date)
            }
        })
        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        rank_id: rank_id,
                        days:selectedDayList.length,
                    },
                    success: function (res) {
                        var list = res.data;
                        self.setData({
                            price: list.data || 0,
                            selectedDayList,
                            dateTimeList: selectedDayList.toString()
                        })
                    }
                })
            },
        })
    },
    // 计算日期
    computeDate(year, month){
        var self = this;
        var url = "/user/already_sell_date";
        var nowDate = this.data.nowDate;
        var week = parseInt(getWeek(year, month));
        var day = parseInt(getDate(year, month));
        var rank_id = this.data.rank_id;
        var days = [];

        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        date: nowDate,
                        rank_id: rank_id,
                    },
                    success: function (res) {
                        var list = res.data;
                        if (list.code == 1) {
                            for (var i = 0; i < week + day; i++) {
                                var date = nowDate + '-' + ((i - week + 1) >= 10 ? (i - week + 1) : "0" + (i - week + 1));
                                days.push({
                                    item: (i < week ? 0 : (i - week + 1)),
                                    date: date,
                                    isBuy: (list.data.indexOf(date) != -1 ),
                                    is_chacked:false
                                })
                            }
                            self.setData({
                                week: week,
                                day: day,
                                days: days
                            })
                        }
                    }
                })
            },
        })
    },
    // 提交订单
    submitOrder(){
        var self = this;
        var url = "/user/choiceDate";
        var type = this.data.type;
        var rank_id = this.data.rank_id
        var auction_id = this.data.auction_id;
        var selectedDayList = this.data.selectedDayList.join(",");

        if (!selectedDayList){
            wx.showToast({
                image:"../../img/warning.png",
                title: '未选择日期',
            })
            return false;
        }

        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        date_time: selectedDayList,
                        rank_id: rank_id,
						auction_id: auction_id,
						type: type
                    },
                    success: function (res) {
                        var list = res.data;
                        if (list.code == 1) {
                            wx.showLoading({
                                title: '跳转中'
                            })
                            wx.navigateTo({
                                url: `../pay-style/pay-style?money=${list.data.price}&order_no=${list.data.trade_no}&order_type=top_order`,
                                success(){
                                    wx.hideLoading();
                                }
                            })
                        }else{
                            wx.showToast({
                                image:"../../img/warning.png",
                                title: list.msg,
                            })
                        }
                    }
                })
            },
        })
    },
    // 选择月份
    changeMouth(e){
        var value = e.detail.value;
        this.computeCalendar(value);
    },
    // 上下翻日历
    changeDate(e){
        var type = e.target.dataset.type;

        var nowDate = this.data.nowDate.split("-");
        var year = parseInt(nowDate[0]);
        var month = parseInt(nowDate[1]);

        if (type == "minus"){
            --month;
            if (month < 1){
                month = 12;
                --year;
            }
        } else if (type == "plus"){
            ++month;
            if (month > 12) { 
                month =  1;
                ++year;
            }
        }
        var value = year + "-" + ( month >= 10 ?  month : "0" + month);
        this.computeCalendar(value)
    },
    // 改变日历时计算日历;
    computeCalendar(value){
        var date = value.split("-");
        var year = date[0];
        var month = date[1];

        this.setData({ 
            nowDate: value,
            dateTimeList:"",
            selectedDayList:[],
            price:0
        });
        this.computeDate(year, month);
    },
})

// 获取日期是星期几
function getWeek(year, month) {
    var nowdate = new Date('' + year + ',' + month + ',01');
    return nowdate.getDay();
}
// 获取日期年、月
function getDate(year, month) {
    var d = new Date(year, month, 0);
    return d.getDate();
}
